<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <liu21st@gmail.com>
// +----------------------------------------------------------------------

// 应用公共文件
/**
 * curl get 请求
 * @param $url
 * @return mixed
 */
function httpGet($url) {
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_TIMEOUT, 500);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($curl, CURLOPT_URL, $url);
    $res = curl_exec($curl);
    curl_close($curl);
    return $res;
}

/**
 * curl post 请求
 * @param $post_data
 * @param $url
 * @return mixed
 */
function httpPost($url,$post_data) {
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_POST, true);
    curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); // 模拟用户使用的浏览器
    curl_setopt($curl, CURLOPT_HEADER, false);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_TIMEOUT, 30);
    curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data);
    $res = curl_exec($curl);
    curl_close($curl);
    return $res;
}

/**
 * curl put 请求
 * @param $Url
 * @param $data
 * @return mixed
 */
function put($url,$data){
    // 创建一个新cURL资源
    $ch = curl_init();
    // 设置URL和相应的选项
    curl_setopt($ch, CURLOPT_URL, $url); //$url设置需要请求的url
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    //设置请求头
    $headerArray = ["Content-type:application/json"];
    curl_setopt($ch,CURLOPT_HTTPHEADER,$headerArray); 
    //设置请求方式和提交的字符串
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST,"PUT"); 
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
    // 抓取URL并把它传递给浏览器
    $output = curl_exec($ch);
    // 关闭cURL资源，并且释放系统资源
    curl_close($ch);
    //print(json_decode($output,true))
}

/**
 * curl delete 请求
 * @param $Url
 * @param $data
 * @return mixed
 */
function delete($url,$data){// 创建一个新cURL资源
    $ch = curl_init();
    // 设置URL和相应的选项
    curl_setopt($ch, CURLOPT_URL, $url); //$url设置需要请求的url
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    //设置请求头
    $headerArray = ["Content-type:application/json"];
    curl_setopt($ch,CURLOPT_HTTPHEADER,$headerArray); 
    //设置请求方式和提交的字符串
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST,"DELETE"); 
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
    // 抓取URL并把它传递给浏览器
    $output = curl_exec($ch);
    // 关闭cURL资源，并且释放系统资源
    curl_close($ch);
    //print(json_decode($output,true))
}
/**
 * curl patch 请求
 * @param $Url
 * @param $data
 * @return mixed
 */
function patch($url,$data){
    // 创建一个新cURL资源
    $ch = curl_init();
    // 设置URL和相应的选项
    curl_setopt($ch, CURLOPT_URL, $url); //$url设置需要请求的url
    //参数设置为 1 是把curl_exec()的结果转为字符串
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    //设置请求头
    $headerArray = ["Content-type:application/json"];
    curl_setopt($ch,CURLOPT_HTTPHEADER,$headerArray); 
    //设置请求方式和提交的字符串
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST,"PATCH"); 
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
    // 抓取URL并把它传递给浏览器
    $output = curl_exec($ch);
    // 关闭cURL资源，并且释放系统资源
    curl_close($ch);
    //print(json_decode($output,true))
}


/**
 * 随机生成数字和字母
 * @param  [type] $length   字符串长度
 * @param  [type] $flag   str:生成字符串，num:生成数字
 */
function getRandStr($length,$flag='str') {
    if($flag=='str'){
        $str     = 'abcdefghijklmnopqrstuvwxyz0123456789';
    }else{
        $str     = '0123456789';
    }
    $len     = strlen($str) - 1;
    $randstr = '';
    for ($i = 0; $i < $length; $i++) {
        $num = mt_rand(0, $len);
        $randstr .= $str[$num];
    }
    return $randstr;
}

/**
 * 字符串截取
 * @param  [type] $str      字符串
 * @param  [type] $position 位置
 * @return [type]           [description]
 */
function sub_str($str,$position){
    $newStr=substr($str,0,strpos($str,$position));
    return $newStr;
}

/**
 * 加密解密方法
 * @param  [type] $str  加密字符串
 * @param  [type] $type 状态1：加密，0:解密
 * @return [type]          [description]
 */
function encryptDecrypt($str,$type=1){
    //密钥
    $key=config('base.token_secret_key');
    if($type){
        //加密
        $encrypted = base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, md5($key), $str, MCRYPT_MODE_CBC, md5(md5($key))));   
    }else{
        //解密
        $decrypted = rtrim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, md5($key), base64_decode($str), MCRYPT_MODE_CBC, md5(md5($key))), "12");   
    }
    return $decrypted;
}

//生成key
function createKey(){
    return 'h'.time();
}

/**
 * 判断变量是否存在
 * @param  [type]  $value       [description]
 * @param  string  $defaultValue 默认值
 * @return boolean               [description]
 */
function isInput($value,$defaultValue=''){
    if(isset($value) && !empty($value)){
        return $value;
    }
    return $defaultValue;
}